<template>
    <div>
       <Row type="flex" justify="space-between" align="middle">
            <Col span="15" class="title">添加模型</Col>
       </Row>
       <div class="table-box" style="overflow: auto;">
           <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
                <FormItem label="名称" prop="name">
                    <Input v-model="formValidate.name" placeholder="输入名称"></Input>
                </FormItem>
                <FormItem label="表名称" prop="tableName">
                    <Input v-model="formValidate.tableName" placeholder="输入表名称"></Input>
                </FormItem>
                <FormItem label="描述" prop="description">
                    <Input v-model="formValidate.description" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="描述···"></Input>
                </FormItem>
                <FormItem>
                    <Button type="primary" @click="handleSubmit('formValidate')">提交添加</Button>
                </FormItem>
            </Form>
       </div>
    </div>
</template>
<script>
    export default {
        data () {

           return {
                formValidate: {
                    name: '',
                    tableName: '',
                    description: ''
                },
                ruleValidate: {
                    name: [
                        { required: true, message: '不能为空！', trigger: 'blur' }
                    ],
                    tableName: [
                        { required: true, trigger: 'blur', validator: function(rule, value, callback){
                            var reg = /^[a-zA-Z]+[_]?[a-zA-Z]+$/;
                            if(!reg.test(value)){
                                callback(new Error('不能为空！必须是a-z、_、A-Z字符组成！'));
                            }
                            callback();
                        } }
                    ],
                    description: [
                        { required: true, message: '不能为空！', trigger: 'blur' }
                    ]
                }
           };
        },
        created: function () {

        },
        mounted() {

        },
        methods: {
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        var that = this;
                        that.$loading.show('添加中');
                        that.$util.ajax({
                            that: that,
                            url: '/model/add/',
                            data: {
                                name: that.formValidate.name,
                                tableName: that.formValidate.tableName,
                                description: that.formValidate.description,
                            },
                            success: function(data){
                                that.$router.go(-1);
                            },
                            fail: function(msg){
                                that.$Message.info(msg);
                            },
                            complete: function(){
                                that.$loading.hide();
                            }
                        })
                    }
                })
            }
        }
    }
</script>
<style scoped>
#cke_editorAddCat {
}
</style>